/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main.LanguageResource;

import java.util.*;

/**
 *
 * @author Isaac Osesina
 */
public abstract class AbstractPhraseSynthesizer<E> implements PhraseSynthesizer{

    private POS_Tagger<E> posTagger;
    private Dictionary<E> dictionary;
    private Map<E, Map<String, Set<String>>> synonymsMap;
    
    public AbstractPhraseSynthesizer(POS_Tagger<E> posTagger, Dictionary<E> dictionary) {
        this.posTagger = posTagger;
        this.dictionary = dictionary;
        synonymsMap = new HashMap<E, Map<String, Set<String>>>();
    }

    public main.LanguageResource.Dictionary<E> getDictionary() {
        return dictionary;
    }

    public POS_Tagger<E> getPosTagger() {
        return posTagger;
    }
    
    
        
    @Override
    public abstract Set<String> synthesize(String phrase);

    
    public void addSynonyms(WordConstruct<E> wc, Set<String> synonyms) {
        if(wc==null || synonyms==null)
            return;
        Map<String, Set<String>> word_synonymSet = synonymsMap.get(wc.getPOS());
        if(word_synonymSet!=null)
            return;
        else
            word_synonymSet = new HashMap<String, Set<String>>();
        Set<String> synonymSet = word_synonymSet.get(wc.getWord().toLowerCase());
        if(synonymSet!=null)
            return;
        else
            synonymSet = new LinkedHashSet<String>(synonyms);
        word_synonymSet.put(wc.getWord().toLowerCase(), synonymSet);
    }
    
    
    public Set<String> getSynonyms(WordConstruct<E> wc) {
        if(wc==null)
            return null;
        
        Map<String, Set<String>> word_synonymSet = synonymsMap.get(wc.getPOS());
        if(word_synonymSet==null)
            return null;
        
        return word_synonymSet.get(wc.getWord().toLowerCase());
    }
    
    
    public Set<String> generateStringCombinations(List<Set<String>> words) {
        if(words==null)
            return null;
        Set<String> phrases = new HashSet<String>();
        for(Set<String> set:words) {
            if(set==null)
                continue;
            Set<String> phrases_tmp = new HashSet<String>();
            for(String s:set) {
                if(s==null)
                    continue;
                if(phrases.isEmpty()) {
                    phrases_tmp.add(s);
                }
                else {
                    for(String phrase:phrases) {
                        phrase += " " + s;
                        phrases_tmp.add(phrase);
                    }
                }
            }
            phrases = phrases_tmp;
        }
        return phrases;
    }
}
